//n = 3, nums = [3, 2, 1]
//n = 4, nums = [2, 2, 3, 1]
n = 12, nums = [2, 8, 13, 9, 7, 6, 7, 4, 1, 4, 10, 12]


function solution(n, nums) {
    nums.sort((a, b) => {
        return a - b;
    })
    let newArr = []
    newArr = nums.filter((item, index, nums) => {
        return index === 0 || item !== nums[index - 1]
    })
    console.log(newArr);

    let len = newArr.length
    if (len < 3) {
        return newArr[len - 1]
    } else {
        return newArr[len - 3]
    }
}
console.log(solution(n, nums));
